/* 
 * url util
 * @author luweijun
 */
(function(){
    function UrlUtil() { };

    UrlUtil.getURLParameters = function(name) {
        var returnUrl = {};
        var sURL = window.document.URL.toString();

        if (sURL.indexOf("?") > 0) {
            var arrParams = sURL.split("?");

            var arrURLParams = arrParams[1].split("&");

            var arrParamNames = new Array(arrURLParams.length);
            var arrParamValues = new Array(arrURLParams.length);

            var i = 0;
            for (i = 0; i < arrURLParams.length; i++) {
                var sParam = arrURLParams[i].split("=");
                arrParamNames[i] = sParam[0];
                if (sParam[1] != "")
                    arrParamValues[i] = sParam[1];
                else
                    arrParamValues[i] = "";
                returnUrl[arrParamNames[i]] = arrParamValues[i];
            }
        }
        if (typeof name == "string") {
            return returnUrl[name] || '';
        }
        return returnUrl;
    };
    
    UrlUtil.getURLParametersForCheckbox = function(name) {
        var returnUrl = {};
        var sURL = decodeURIComponent(window.document.URL.toString());

        if (sURL.indexOf("?") > 0) {
            var arrParams = sURL.split("?");

            var arrURLParams = arrParams[1].split("&");

            var arrParamNames = new Array(arrURLParams.length);
            var arrParamValues = new Array(arrURLParams.length);

            var i = 0;
            for (i = 0; i < arrURLParams.length; i++) {
                var sParam = arrURLParams[i].split("=");
                arrParamNames[i] = sParam[0];
                if (sParam[1] != "")
                    arrParamValues[i] = sParam[1];
                else
                    arrParamValues[i] = "";

		if (typeof returnUrl[arrParamNames[i]] == "undefined") 
			returnUrl[arrParamNames[i]] = [];
                returnUrl[arrParamNames[i]].push(arrParamValues[i]);
            }
        }
        if (typeof name == "string") {
            return returnUrl[name] || [];
        }
        return returnUrl;
    };

    UrlUtil.inParameters = function(para,requests) {
	var exist = false;
	if (typeof para != "undefined" && typeof requests == "object" && Array.isArray(requests)) {
		for (var i=0,len=requests.length; i<len; i++) {
			if (para == requests[i]) {
				exist = true;
				break;
			}
		}
	}
	return exist;
    };


    UrlUtil.addParameter = function(href, name, value) {

        var delimiter;
        if (href.indexOf("?") == -1)
            delimiter = "?";
        else
            delimiter = "&";
        return href + delimiter + name + "=" + value;
    };


    /** 
     * remove the parameter from an url
     */
    UrlUtil.removeParameter = function(href, name) {

        var tmp = href.split("?");

        var newHref = tmp[0];
        var query = new Array();
        if (tmp[1]) {
            var paramObj = tmp[1].split("&");

            for (var i = 0; i < paramObj.length; i++) {
                if (paramObj[i].indexOf(name + "=") != 0) {
                    query.push(paramObj[i]);
                }
            }
        }

        if (query.length > 0) {
            newHref += "?";

            for (var i = 0; i < query.length; i++) {
                newHref += query[i];

                if (i + 1 < query.length) {
                    newHref += "&";
                }
            }
        }

        return newHref;
    };

    /**
     * Is this an external URL containing
     */
    UrlUtil.isExternal = function(url) {
        return url.indexOf("://") != -1;
    };
    
    umams.admin.Url = UrlUtil;
    
})();


